package com.admin.controller;

import com.admin.entity.Goods;
import com.admin.service.GoodsService;
import com.admin.service.StorageService;
import com.admin.entity.Storage;
import com.admin.entity.Supplier;
import com.admin.service.SupplierService;
import com.admin.entity.SysUser;
import com.admin.service.SysUserService;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.utils.admin.ShiroUtils;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.base.entity.DateUtils;
import com.admin.sys.utils.excel.ExportExcel;
import com.admin.sys.utils.excel.ImportExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*【入库信息】页面接口
*/
@RestController
@RequestMapping("/admin/storage")
public class StorageController extends BaseController {
    @Autowired
    private StorageService storageService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private SupplierService supplierService;

    /**
    * 【入库信息】根据条件分页查询
    * @param page
    * @param storage
    * @return
    */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<Storage> page, Storage storage) {
        IPage<Storage> iPage = storageService.getPage(page, storage);
        return ResultInfo.ok("获取分页成功", iPage);
    }

    /**
    * 【入库信息】根据条件查询
    * @param storage
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Storage storage) {
        List<Storage> list = storageService.getList(storage);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    * 【入库信息】根据对象查询
    * @param storage
    * @return
    */
    @RequestMapping("/get")
    public ResultInfo get(Storage storage) {
        Storage entity = storageService.get(storage);
        return ResultInfo.ok("获取对象成功", entity);
    }

    /**
    * 【入库信息】提交(新增或修改)
    * @param storage
    * @return
    */
    @RequestMapping("/sub")
    public ResultInfo insert(Storage storage) {
        if (StringUtils.isEmpty(storage.getId())) { //新增
            storage.setUserId(ShiroUtils.getCurrentUserId());
            storage.setStorageTime(new Date());
            storageService.insert(storage);
        } else {//修改
            storageService.update(storage);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
    * 【入库信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    public ResultInfo delete(String id) {
        storageService.delete(id);
        return ResultInfo.ok("删除成功!");
    }

    /**
    * 【入库信息】批量删除
    * @param ids
    * @return
    */
    @RequestMapping("/delAll")
    public ResultInfo delAll(String[] ids) {
        storageService.delAll(ids);
        return ResultInfo.ok("批量删除成功！");
    }


    /**
    *【入库信息】导出
    */
    @RequestMapping(value = "/export")
    public void exportFile(Storage storage, HttpServletResponse response) {
        try {
            String fileName = "入库信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<Storage> list = storageService.getList(storage);
            for (Storage entity : list) {
                SysUser sysUser = sysUserService.get(entity.getUserId());
                entity.setUserId(sysUser.getUserName());
                Goods goods = goodsService.get(entity.getGoodsId());
                entity.setGoodsId(goods.getName());
                Supplier supplier = supplierService.get(entity.getSupplierId());
                entity.setSupplierId(supplier.getName());
            }
            new ExportExcel("入库信息", Storage.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    *【入库信息】导入
    */
    @RequestMapping(value = "/import")
    public ResultInfo importFile(MultipartFile file) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<Storage> list = ei.getDataList(Storage.class);
            for (Storage storage : list) {
                try {
                    storageService.insert(storage);
                    successNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条入库信息记录。");
            }
            return ResultInfo.ok("已成功导入 " + successNum + " 条入库信息记录" + failureMsg);
        } catch (Exception e) {
            return ResultInfo.error("导入入库信息失败！失败信息：" + e.getMessage());
        }
    }

    /**
    *【入库信息】模板下载
    */
    @RequestMapping(value = "/import/template")
    public ResultInfo importFileTemplate(HttpServletResponse response) {
        try {
            String fileName = "入库信息数据导入模板.xlsx";
            List<Storage> list = new ArrayList<>();
            new ExportExcel("入库信息数据", Storage.class, 1).setDataList(list).write(response, fileName).dispose();
            return ResultInfo.ok("下载模板成功！");
        } catch (Exception e) {
            return ResultInfo.error("导入模板下载失败！失败信息：" + e.getMessage());
        }
    }

}



